package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class CourseAudienceHelper extends NewAudienceHelper {
	private static final long serialVersionUID = -3240652197020178384L;

	public CourseAudienceHelper(ORMInterface orm) {
		List<Universities> univList = new UniversitiesDAO(orm)
				.getAllUniversities();
		List<Courses> cList = new ArrayList<Courses>();
		for (Universities univ : univList) {
			cList.addAll(new CoursesDAO(orm).getUnivCourses(univ));
		}
		for (Courses c : cList) {
			CourseInstances cInst = new CourseInstancesDAO(orm).getActiveInst(c);
			if(cInst != null)
				courseList.add(cInst);
		}
	}

	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		List<IModel<CourseInstances>> temp = courseList.getObject();
		for (IModel<CourseInstances> t : temp) {
			Set<String> keys = roles.keySet();
			for (String r : keys)
				list.add("[" + r +" of " + t.getObject().getTitle()+ "];");
		}
		return list;
	}

	public IModel<CourseInstances> getCourse(String s) {
		List<IModel<CourseInstances>> ci = courseList.getObject();
		for (IModel<CourseInstances> c : ci) {
			if (s.contains(c.getObject().getTitle()) && s.contains("[") && s.contains("]"))
				return c;
		}
		return null;
	}

	public CayenneList<Users> getAllUsers(String s) {
		IModel<CourseInstances> ci = getCourse(s);
		Set<Entry<String, Integer>> set = roles.entrySet();
		for (Entry<String, Integer> e : set) {
			if (s.contains(e.getKey())) {
				return new CourseDataHandler(ci).getPeopleByRole(e.getValue());
			}
		}
		return new CayenneList<Users>(null);
	}
	
	public int getRoleId(String s){
		Set<String> set = roles.keySet();
		for(String role:set){
			if(s.contains(role)){
				return roles.get(role);
			}
		}
		return 0;
	}

	public CayenneList<GenericAudience> getAudiences(String s) {
		return null;
	}

	public static HashMap<String, Integer> roles = new HashMap<String, Integer>();
	static {
		roles.put("Faculty", Enrolments.TEACHER);
		roles.put("Teaching Assistants", Enrolments.TEACHING_ASSISTANT);
		roles.put("Students", Enrolments.STUDENT);
		roles.put("Audit Students", Enrolments.OBSERVER);
	}

	private UniqueElementList<CourseInstances> courseList = 
		new UniqueElementList<CourseInstances>(null);

	@Override
	public String getNameForAudience(String s) {
		Set<String> set = roles.keySet();
		for(String role:set){
			if(s.contains(role)){
				return role + ", "+getCourse(s).getObject().getTitle();
			}
		}
		return "";
	}
}
